let c = []
let N = 0
let lowbit = n => n & -n
let query = x => {
  let ans = 0
  while (x > 0) {
    ans += c[x]
    x -= lowbit(x)
  }
  return ans
}
let add = (x, v) => {
  while (x <= N) {
    c[x] += v
    x += lowbit(x)
  }
}
function main(nums, n) {
  let res1 = 0,
    res2 = 0
  N = n
  c = Array(n + 1).fill(0)
  let less = Array(n).fill(0)
  let great = Array(n).fill(0)
  for (let i = 0; i < n; i++) {
    /*
      初始化数组li为[0,0,...,0],循环遍历原数组nums，给数组li的第nums[i]项+1
      此时数组li变为[0,0,...1,...0] 1所在位置为nums[i]
      此时数组li的从 0到nums[i]索引位置值=1的情况都是数组nums从0到i中小于nums[i]的数
      即sum li(nums[i]-1)就是左侧比nums[i]小的数
      即sum li(n)-sum li(nums[i]-1)就是左侧比nums[i]大的数 sum li(n)就是此时一共遍历了的个数 sum li(n) == i
      假设现在nums[i]=5的数据出现，此时li[5]左侧有一个1，右侧有3个1,这四个数字，都是在i进入前进入统计数组的，所以它们4个都是描述的i之前的现实数据情况
      用数组c表示数组li的前缀和，用数组数组方式存储、更新前缀和数组
    */
    let x = nums[i]
    let a = query(x - 1)
    let b = query(n)
    less[i] = a
    great[i] = b - a
    add(x, 1)
  }
  c = Array(n + 1).fill(0)
  for (let i = n - 1; i >= 0; i--) {
    let x = nums[i]
    res1 += less[i] * query(x - 1)
    res2 += great[i] * (query(n) - query(x - 1))
    add(x, 1)
  }
  return [res2, res1]
}

let nums = [1, 5, 3, 2, 4]
let n = 5
console.log(main(nums, n))
